package com.anXin.domestic.service.impl;

import cn.hutool.json.JSONUtil;
import com.anXin.apis.user.IUserClient;
import com.anXin.domestic.mapper.DomesticOrdersMapper;
import com.anXin.domestic.service.DomesticOrdersService;
import com.anXin.domestic.service.DomesticResortService;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.service.dtos.DomesticOrdersDTO;
import com.anXin.model.service.pojos.DomesticOrders;
import com.anXin.model.service.pojos.DomesticResort;
import com.anXin.model.user.dtos.UserInfoDto;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class DomesticOrdersServiceImpl extends ServiceImpl<DomesticOrdersMapper, DomesticOrders> implements DomesticOrdersService {

    @Autowired
    private DomesticResortService domesticResortService;
    @Resource
    private IUserClient iUserClient;

    /**
     * 生成/接收订单
     * @param domesticOrdersDTO
     * @return
     */
    @Override
    public ResponseResult addOrders(DomesticOrdersDTO domesticOrdersDTO) {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        if (domesticOrdersDTO.getServerId().equals(user.getId())) {
            DomesticResort domesticResort = domesticResortService.getById(domesticOrdersDTO.getPostId());
            domesticResort.setState(3);
            domesticResortService.updateById(domesticResort);
            DomesticOrders domesticOrders = new DomesticOrders();
            domesticOrders.setClientId(domesticResort.getAuthorId());
            domesticOrders.setClientPhone(domesticResort.getAuthorPhone());
            domesticOrders.setClientAddress(domesticResort.getAuthorAddress());
            domesticOrders.setServerId(domesticOrdersDTO.getServerId());
            domesticOrders.setServerPhone(user.getPhone());
            domesticOrders.setState(0);
            domesticOrders.setCreatedTime(new Date());
            domesticOrders.setPostId(domesticOrdersDTO.getPostId());
            domesticOrders.setTitle(domesticResort.getTitle());
            domesticOrders.setContent(domesticResort.getContent());
            this.save(domesticOrders);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), "订单已接单成功!");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(), "无权限操作");
        }
    }

    /**
     * 根据订单id查询订单
     * @param orderId
     * @return
     */
    @Override
    public ResponseResult getOrders(Long orderId) {
        DomesticOrders domesticOrders = this.getById(orderId);
        return ResponseResult.okResult(domesticOrders);
    }

    /**
     * 客户查看自己所有的订单
     * @return
     */
    @Override
    public ResponseResult getClientOrders() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<DomesticOrders> domesticOrders = query().eq("client_id",user.getId()).list();
        return ResponseResult.okResult(domesticOrders);
    }

    /**
     * 服务人员查看自己的接单
     * @return
     */
    @Override
    public ResponseResult getServerOrders() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<DomesticOrders> domesticOrders = query().eq("server_id",user.getId()).list();
        return ResponseResult.okResult(domesticOrders);
    }

    /**
     * 取消订单
     * @param postId
     * @return
     */
    @Override
    public ResponseResult cancelOrders(Long postId) {
        DomesticResort domesticResort = domesticResortService.getById(postId);
        domesticResort.setState(2);
        domesticResortService.updateById(domesticResort);
        DomesticOrders domesticOrders = query().eq("post_id",postId).one();
        domesticOrders.setState(2);
        this.updateById(domesticOrders);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"取消订单成功!");
    }
}
